package com.example.tests;

import java.util.List;
import org.apache.commons.lang3.builder.CompareToBuilder;
import com.example.utils.SortedListOf;

public class ContactDataRow implements Comparable<ContactDataRow>{
	
	public String firstname;
	public String lastname;
	public String phone;
	public String email;

	public ContactDataRow() {
	}
		
	@Override
	public String toString() {
		return "ContactData [firstname=" + firstname + ", lastname=" + lastname
				+ ", phone=" + phone + ", email=" + email + "]";
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ContactDataRow other = (ContactDataRow) obj;
		return this.toString().equals(other.toString());
	}

	@Override 
	public int compareTo(ContactDataRow other) {
		return new CompareToBuilder()		
		.append(this.firstname, other.firstname)
		.append(this.lastname, other.lastname)		
		.append(this.phone, other.phone)
		.append(this.email, other.email)
		.toComparison();
	}
	
	public static ContactDataRow buildFromContact(ContactData contact){
		ContactDataRow result = new ContactDataRow();
		
		// to avoid the error
		result.firstname = contact.getLastname();
		result.lastname = contact.getFirstname();
		
		// to catch the error
		//result.firstname = contact.firstname;
		//result.lastname = contact.lastname;
		
		result.phone = contact.getHome() != null && !contact.getHome().isEmpty()? contact.getHome(): 
			           contact.getMobile() != null && !contact.getMobile().isEmpty()? contact.getMobile(): contact.getWork();
		result.email = contact.getEmail() != null && !contact.getEmail().isEmpty()? contact.getEmail(): contact.getEmail2();
		return result;
	}

	public ContactDataRow withFirstname(String firstname) {
		this.firstname = firstname;
		return this;
	}
	
	public ContactDataRow withLastname(String lastname) {
		this.lastname = lastname;
		return this;
	}	
	
	public ContactDataRow withPhone(String phone) {
		this.phone = phone;
		return this;
	}
	
	public ContactDataRow withEmail(String email) {
		this.email = email;
		return this;
	}

	public String getFirstname() {
		return firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public String getPhone() {
		return phone;
	}

	public String getEmail() {
		return email;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	
	public static SortedListOf<ContactDataRow> buildFromContactList(List<ContactData> contactList) { 
		SortedListOf<ContactDataRow> result = new SortedListOf<ContactDataRow>();             
		for (ContactData c : contactList) {             
			result.add(ContactDataRow.buildFromContact(c));     
			}         
		return result;        
		}
	
}